/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.fimuni.pa165.restfrontend.validator;

import cz.fimuni.pa165.backendapi.dto.CarDTO;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 *
 * @author Ondřej Myška
 */

@Component("carValidator")
public class CarValidator implements Validator{

    @Override
    public boolean supports(Class clazz) {
        return CarDTO.class.equals(clazz);
    }

    @Override
    public void validate(Object o, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "spz", "car.spz.error");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "colour", "car.colour.error");
        
   
        CarDTO car = (CarDTO)o;
        
        if(car.getCompany()==null){
            errors.rejectValue("company", "car.company.error");
        }
    }
    
}